﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Blade.DiskEdit.Properties;
using Blade.Devices.Storage;

namespace Blade.DiskEdit {
	public partial class MainForm : Form {
		DiskImageDevice image;
		PartitionTable partitions;

		public MainForm() {
			InitializeComponent();
			tree.ImageList = new ImageList {
				TransparentColor = Color.Magenta,
				ImageSize = new Size(16, 16),
				Images = { 
					Resources.folder,
					Resources.chart_pie
				},
			};
			foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
				if (!assembly.FullName.StartsWith("Blade.FileSystem."))
					continue;
				foreach (var type in assembly.GetTypes()) {
					if (type.IsSubclassOf(typeof(FileSystemBase))) {
						filesystemMenu.Items.Add(
							new ToolStripMenuItem(
								((DescriptionAttribute)TypeDescriptor
								.GetAttributes(type)[typeof(DescriptionAttribute)]).Description
							) { Tag = type }
						);
					}
				}
			}
		}

		private void openImageMenuItem_Click(object sender, EventArgs e) {
			if (openImageDialog.ShowDialog(this) != DialogResult.OK)
				return;
			var stream = File.Open(openImageDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
			image = new DiskImageDevice(stream, 512);
			partitions = image.LoadPartitionTable();
			tree.BeginUpdate();
			tree.Nodes.Clear();
			foreach (var p in partitions) {
				tree.Nodes.Add(
					new PartitionNode(p) {
						Nodes = { new DummyNode() }
					}
				);
			}
			tree.EndUpdate();
		}

		private void tree_BeforeExpand(object sender, TreeViewCancelEventArgs e) {

		}

		private void addPartitionToolStripMenuItem_Click(object sender, EventArgs e) {
		}
	}
}
